﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Main
{
    public partial class CreateNewUser : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                if (Session["LIMITEDUSER"] == null)
                {
                    if (!IsPostBack)
                    {
                        ControlDomains.Control control = new ControlDomains.Control();
                        ListExistingDomains(control.ViewDomainName(Session["Company"].ToString()));
                    }
                }
                else if (Session["LIMITEDUSER"] == "true")
                {
                    Response.Redirect("Default.aspx");
                }

            }
            else
                Response.Redirect("Login.aspx"); 
        }

        /// <summary>
        /// triggers listing of existing verified domains on SQL Azure
        /// </summary>
        /// <param name="list"></param>
        private void ListExistingDomains(List<VerifiedDomain> list)
        {
            foreach (var Result in list)
            {
                if (list.Count == 0)
                {
                    ddlDomain.Items.Add("No existing domains");
                    break;
                }
                else
                    ddlDomain.Items.Add(Result.DomainName);
            }
        }

        /// <summary>
        /// triggers creation of a new user on SQL Azure and Office 365
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != String.Empty && txtSurname.Text != String.Empty && txtPersonalEmail.Text != String.Empty && ddlDomain.SelectedValue != String.Empty)
                {
                    bool adminUser = false;
                    if (chkIsAdmin.Checked == true)
                        adminUser = true;

                    ControlCommon common = new ControlCommon();
                    ControlUsers.Control control = new ControlUsers.Control();
                    String msg = control.CreateUser(common.ConvertToEnglish(txtName.Text.Trim()), common.ConvertToEnglish(txtSurname.Text.Trim()), common.ConvertToEnglish(txtPersonalEmail.Text.ToLower().Trim()), ddlDomain.SelectedValue, adminUser, Session["Company"].ToString());

                    if (msg == String.Empty)
                    {
                        lblError.Text = "New user has been successfully created!";
                    }
                    else
                        lblError.Text = msg;
                }
                else
                    lblError.Text = "Please give all the required information!";
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }

        /// <summary>
        /// makes all fields blank
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = String.Empty;
            txtSurname.Text = String.Empty;
            txtPersonalEmail.Text = String.Empty;
            chkIsAdmin.Checked = false;
            lblError.Text = String.Empty;
        }
    }
}